git config --global user.namealicebyers5
Alice Byers, Data Innovation Team
Scottish Government
These slides aim to demonstrate the use of Git and GitHub in the Scottish Government when working in the SCOTS network.
An understanding of the concepts of version control, Git and GitHub is assumed. More information can be found in the Introduction to Version Control slides.
Further guidance and resources for using version control on SCOTS are available on the Statistics Group sharepoint site.
Make an iFix request for “Git version 2.21.0.windows.1”
Git is a free and open-source software and therefore does not incur a cost to install
Git Bash is a command line interface for using Git
It is installed with Git
Some basic commands include:
pwd; prints present working directory
ls; lists files contained in working directory
cd <filepath>; changes working directory
ONS Learning Hub has further training on Command Line Basics
Your name and email is linked to every commit you make using Git and cannot be changed retrospectively, therefore it is important to do this.
If you plan to use GitHub, make sure your user name and email address match those associated with your GitHub account.
To do this in Git Bash, run:
To check this has worked:
Install Git via iFix
Set name and email address in Git Bash
Open Git Bash from Windows start menu
Use cd command to change directory to your working folder
git init to initiate Git. This will create a .git folder within your project. (Note: you only need to do this once per project.)A gitignore file defines what files should not be tracked by Git. This is especially important if you plan on using GitHub as sensitive information should not be made available there.
Generally, the following should be ignored:
Data files
Passwords or credentials
Code that contains sensitive information
Configuration files
To tell Git to ignore these files:
Create a new file in your directory called .gitignore. This can be done in the usual way in File Explorer, or by using the touch command in Git Bash.
Open the .gitignore file in a text editor (or R) and add names of folders and files to be ignored.
git status to show a summary of your Git repository - run this often to check that your other git commands have done what you expect them to dodata.csv is not listed here. This is because we have told Git to ignore csv files in the gitignore.Use git add to ‘stage’ files for the next commit
Either list the files you’d like to stage; e.g. git add code.R, or
To stage all tracked files, use a full stop; e.g. git add .
git status to check that the correct files have been stagedUse git commit to add the files to the Git history.
git status again shows that there are no further changes to commitgit log --oneline will give a short summary of the commit historycode.R. Add some commented lines to give the script a title and description.git status to check that Git has recognised the changegit diff to inspect what changes have been made to code.R. Green text highlights additions and red text highlights deletions.git add and git commit to stage and commit the change to code.R.git log to view the Git history - there are now two commitsCommit early and often (especially when you’re still learning)
Write commit messages that make sense (your future self and colleagues will thank you)
Run git status often (especially when you’re still learning)
Check which files have changes tracked
Check you have staged the correct files
Check files that should be ignored are not being tracked (and committed!)
Make a change
git add
git commit
git status often
Not all Git functionality is available from RStudio, but it can be more user-friendly and convenient if you’re working with R
Changes are listed in the Git pane (usually in the top right window)
Like Git Bash, you need to both stage and commit the change
To stage, tick the box next to each file you’d like to add (top-left pane)
To commit, enter a message and click ‘Commit’ (top-right pane)
Use your work email address
Enable two-factor authentication
Add a (work appropriate) photo and username
Add your contact details and employer
Make your gov.scot email address public on your profile
Work projects should all be hosted from the Data Science Scotland organisation
Request to join the organisation
Once your request has been accepted, you will be free to create Git repositories in Data Science Scotland
Select Data Science Scotland as owner for work projects
Give the repository a name
Choose whether to make your repository public or private
Add a README file
Click the green ‘Create repository’ button
A ‘remote’ is a version of your Git repository hosted on the internet or network somewhere.
This should be thought of as the main place where your repository is stored.
Most commonly, GitHub is used to host remote repositories. But, it can also be a folder on an internal shared network.
Users take a copy (‘clone’) of the repository from the remote
Users regularly ‘push’ their changes back to the remote so other users have access to the latest version
Users regularly ‘pull’ from the remote to ensure they are working with the latest version
flowchart TB R[Remote] R <--> U1((<font size=2>User 1)) R <--> U2((<font size=2>User 2)) R <--> U3((<font size=2>User 3)) linkStyle default stroke-width:1px;
Generate an SSH key in Git Bash
(this should be the email registered with your Github account)
Git Bash will ask where you want to create the key. The default location is recommended. (You can find your SSH home in Git Bash by running: echo ~/.ssh.)
Git Bash will ask if you want to set a passphrase. This is a local password that will be requested each time you use the key. It is OK not to set a passphrase here by just pressing enter twice.
Copy the SSH key:
Copy the returned value from Git Bash.
Now you have an SSH key setup, you should use the SSH URL to clone repositories from GitHub.
Click the green ‘Code’ button and under ‘Clone’, select ‘SSH’, and copy the address.
Open Git Bash and navigate to the directory you’d like to clone the repository to
Use git clone to clone the repository
Change directory to the cloned repository using cd
Add a new file
git add
git commit
‘Push’ the commit to GitHub (the remote) using git push
Note that git status now says the repository is up to date with origin/main (this is another name for the remote repository)
‘Pull’ from GitHub regularly to ensure your local copy of the repository is up to date (especially if other people are also working on the repository)
Note that there is now an extra commit when running git log
Use SSH keys to connect to GitHub from Git Bash
git push often
git pull often
Main branch?
Branches allow users to make changes to files without affecting the ‘production-ready’ main branch
Allows changes to be peer reviewed before being merged into main branch
More information on branching in the Duck book
Alice Byers, Data Innovation Team
Scottish Government